package com.lkd.http.controller;
import com.google.common.base.Strings;
import com.lkd.config.WXConfig;
import com.lkd.exception.LogicException;
import com.lkd.feign.OrderService;
import com.lkd.utils.OpenIDUtil;
import com.lkd.vo.OrderVO;
import com.lkd.vo.Pager;
import com.lkd.vo.PayReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {


    @Autowired
    private WXConfig wxConfig;

    /**
     * 获取openId
     * @param jsCode
     * @return
     */
    @GetMapping("/openid/{jsCode}")
    public String getOpenid(@PathVariable("jsCode")  String jsCode){
        String openId = OpenIDUtil.getOpenId(wxConfig.getAppId(), wxConfig.getAppSecret(), jsCode);
        log.info("openId:{} ,jsCode:{}",openId,jsCode);
        return openId;
    }

    @Autowired
    private OrderService orderService;

    /**
     * 小程序/屏幕端请求支付
     * @param payVO
     * @return
     */
    @PostMapping("/requestPay")
    public String requestPay(@RequestBody PayReq payVO){

        // 1.通过小程序传递的jsCode获得用户openid
        if (!(StringUtils.isEmpty(payVO.getJscode()))) {
            String openId = OpenIDUtil.getOpenId(wxConfig.getAppId(), wxConfig.getAppSecret(), payVO.getJscode());
            payVO.setOpenId(openId);
        }

        // 2.调用订单服务创建订单并完成支付统一下单
        String responseData = orderService.weixinPay(payVO);
        if(Strings.isNullOrEmpty(responseData)){
            throw new LogicException("微信支付接口调用失败");
        }
        return responseData;
    }



    /**
     * 订单搜索
     * @param pageIndex
     * @param pageSize
     * @param orderNo
     * @param openId
     * @param startDate
     * @param endDate
     * @return
     */
    @GetMapping("/search")
    public Pager<OrderVO> search(
            @RequestParam(value = "pageIndex",required = false,defaultValue = "1") Integer pageIndex,
            @RequestParam(value = "pageSize",required = false,defaultValue = "10") Integer pageSize,
            @RequestParam(value = "orderNo",required = false,defaultValue = "") String orderNo,
            @RequestParam(value = "openId",required = false,defaultValue = "") String openId,
            @RequestParam(value = "startDate",required = false,defaultValue = "") String startDate,
            @RequestParam(value = "endDate",required = false,defaultValue = "") String endDate){
        return orderService.search(pageIndex,pageSize,orderNo,openId,startDate,endDate);
    }





}
